﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Weather.Interfaces;
using DispatchAssist.Weather.Entities;

namespace DispatchAssist.Weather.Controls
{
    /// <summary>
    /// 收藏夹树节点
    /// </summary>
    /// <remarks>
    /// 1.父节点不许被用户删除，其他叶子节点可被用户删除
    /// 2.可由用户添加子节点
    /// 3.支持排序显示
    /// 4.本地缓存
    /// </remarks>
    class FavoriteTreeNode : TreeNode
    {
        #region 属性

        /// <summary>
        /// 是否为父节点
        /// </summary>
        public bool IsParent { get { return this.Nodes.Count > 0; } }


        public string TypeName { get; set; }


        private IWeatherMapInfo mapInfo;
        public IWeatherMapInfo MapInfo
        {
            get { return mapInfo; }
        }
        #endregion

        #region 构造

        protected FavoriteTreeNode(string name)
            : base(name)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="index"></param>
        /// <param name="info"></param>
        public FavoriteTreeNode(string name, IWeatherMapInfo info)
            : this(name)
        {
            this.mapInfo = info;

            this.TypeName = name;
            this.Tag = info;
        }

        #endregion

        #region 方法
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public void AddChild(IWeatherMapInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("type");

            this.Nodes.Add(new FavoriteTreeNode(info.Name, info));
        }
        /// <summary>
        /// 移动位置
        /// </summary>
        /// <param name="info"></param>
        /// <param name="newIndex">从0开始</param>
        public void MoveChild(IWeatherMapInfo info, int newIndex)
        {
            if (info == null)
                throw new ArgumentNullException("type");

            if (newIndex < 0 || newIndex >= Nodes.Count)
                throw new ArgumentOutOfRangeException("newIndex");

            TreeNode node = null;
            foreach (FavoriteTreeNode n in this.Nodes)
            {
                if (n.MapInfo == info)
                {
                    node = n;
                    break;
                }
            }
            if (node == null)
                return;

            this.Nodes.Remove(node);
            this.Nodes.Insert(newIndex, node);

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="info"></param>
        public void DeleteChild(IWeatherMapInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("type");


            foreach (FavoriteTreeNode node in this.Nodes)
            {
                if (node.MapInfo == info)
                {
                    this.Nodes.Remove(node);
                }
            }
        }
        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="newName"></param>
        public void ChangeName(string newName)
        {
            if (string.IsNullOrEmpty(newName))
                return;

            this.TypeName = this.Text = newName;

        }
        /// <summary>
        /// 返回所有收藏夹中的数据
        /// </summary>
        /// <returns></returns>
        public IList<IWeatherMapType> GetAllFavorites()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
